#import "NSManagedObjectExtenstion.h"
#import "NSDateExtension.h"

@implementation NSManagedObject (safeSetValuesKeysWithDictionary)

- (void)safeSetValuesForKeysWithDictionary:(NSDictionary *)keyedValues
{
	@try {
		NSDictionary *attributes = [[self entity] attributesByName];
		for (NSString *attribute in attributes) {
			id value = [keyedValues objectForKey:attribute];
			if (value == nil) {
				continue;
			}
			NSAttributeType attributeType = [[attributes objectForKey:attribute] attributeType];
			//NSLog(@"value:'%@' attrib:'%@'",value, attribute);
			if ((attributeType == NSStringAttributeType) && ([value isKindOfClass:[NSNumber class]])) {
				value = [value stringValue];
			} else if (((attributeType == NSInteger16AttributeType) || (attributeType == NSInteger32AttributeType) || (attributeType == NSInteger64AttributeType) || (attributeType == NSBooleanAttributeType)) && ([value isKindOfClass:[NSString class]])) {
				value = [NSNumber numberWithInteger:[value  integerValue]];
			} else if ((attributeType == NSFloatAttributeType) && ([value isKindOfClass:[NSString class]])) {
				value = [NSNumber numberWithDouble:[value doubleValue]];
			} else if ((attributeType == NSDateAttributeType) && ([value isKindOfClass:[NSString class]])) {
				value = [NSDate getDateFromJSON:value];
			}
			if (value == [NSNull null]) {
				[self setValue:nil forKey:attribute];
			}
			else {
				[self setValue:value forKey:attribute];
			}        
		}
	}
	@catch (NSException * e) {
		NSLog(@"ERROR: safeSetValuesForKeysWithDictionary:%@",[e description]);
	}    
}

- (void)safeSetValuesForKeysWithDictionary:(NSDictionary *)keyedValues dateFormatter:(NSDateFormatter *)dateFormatter
{
    NSDictionary *attributes = [[self entity] attributesByName];
    for (NSString *attribute in attributes) {
        id value = [keyedValues objectForKey:attribute];
        if (value == nil) {
            continue;
        }
        NSAttributeType attributeType = [[attributes objectForKey:attribute] attributeType];
        if ((attributeType == NSStringAttributeType) && ([value isKindOfClass:[NSNumber class]])) {
            value = [value stringValue];
        } else if (((attributeType == NSInteger16AttributeType) || (attributeType == NSInteger32AttributeType) || (attributeType == NSInteger64AttributeType) || (attributeType == NSBooleanAttributeType)) && ([value isKindOfClass:[NSString class]])) {
            value = [NSNumber numberWithInteger:[value integerValue]];
        } else if ((attributeType == NSFloatAttributeType) &&  ([value isKindOfClass:[NSString class]])) {
            value = [NSNumber numberWithDouble:[value doubleValue]];
        } else if ((attributeType == NSDateAttributeType) && ([value isKindOfClass:[NSString class]]) && (dateFormatter != nil)) {
            value = [dateFormatter dateFromString:value];
        }
        [self setValue:value forKey:attribute];
    }
}

-(NSDictionary*) getJSONString
{
	NSMutableDictionary *fields = [NSMutableDictionary dictionary];
	for (NSAttributeDescription *attribute in [[self entity] properties]) {
		NSString *attributeName = attribute.name;
		id attributeValue = [self valueForKey:attributeName];
		if (attributeValue) {
			if ([attributeValue isKindOfClass:[NSDate class]]) {
				[fields setObject:[(NSDate*)attributeValue getJSONDateString] forKey:attributeName];
			}
			else if([attributeValue isKindOfClass:[NSManagedObject class]])
			{
				//[fields setObject:[attributeValue getJSONString] forKey:attributeName];
			}
			else if([attributeValue isKindOfClass:[NSSet class]])
			{
				NSArray *objs = [(NSSet*)attributeValue allObjects];
				NSMutableArray *dics = [[NSMutableArray alloc] init];
				for (NSManagedObject *nmo in objs) {
					[dics addObject:[nmo getJSONString]];
				}
				[fields setObject:dics forKey:attributeName];
			}

			else
			{
				[fields setObject:attributeValue forKey:attributeName];               
			}
		}
	}
	return fields;
}
@end